﻿// Matt's TinyMVC Copyright (c) 2009 Matt J. Crouch

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all copies
// or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using MattsTinyMVC;
using System.Web;

namespace PurchasingSampleApplication
{
    [Serializable]
    public class PurchaseOrder : BaseModel
    {

        private ShippingMethod _previousShipVia;

        public PurchaseOrder() : base()
        {
            PONumber = Math.Abs(Guid.NewGuid().GetHashCode());
            DateCreated = DateTime.Now;
            ShipToName = "My Company";
            ShipToAddress = "123 Main Street";
            TaxRate = 0.08M;
            ShipVia = ShippingMethod.SecondDay;
            _previousShipVia = ShipVia;
        }

        public void UpdateShippingMethod()
        {
            PurchasingDepartmentController controller = (PurchasingDepartmentController)this.Parent;
            if (_previousShipVia != ShipVia)
            {
                controller.StatusMessage = "Changed the shipping method to: " + ShipVia.ToString();
                _previousShipVia = ShipVia;
            }
        }

        [TrackModels]
        internal ObservableCollection<OrderLineItem> _lineItems = null;
        public IEnumerable<OrderLineItem> LineItems
        {
            get { return _lineItems; }
        }

        public void AddLineItem()
        {
            OrderLineItem newLineItem = new OrderLineItem();
            newLineItem.ItemNumber = Guid.NewGuid().ToString().Substring(0, 5);
            newLineItem.Description = "TinyMVC Test Item, Model #" + newLineItem.ItemNumber;
            newLineItem.Quantity = new Random(Environment.TickCount).Next(1, 5);
            newLineItem.UnitPrice = 29.99M;
            _lineItems.Add(newLineItem);
        }

        public int PONumber { get; set; }
        public DateTime DateCreated { get; set; }
        public string ShipToName { get; set; }
        public string ShipToAddress { get; set; }
        public string Requisitioner { get; set; }
        public ShippingMethod ShipVia { get; set; }

        public decimal Subtotal { get; set; }
        public decimal TaxRate { get; private set; }
        public decimal Tax { get; set; }
        public decimal ShippingAndHandling { get; set; }
        public decimal Total { get; set; }

    }


}